<?php
declare(strict_types=1);

namespace app\controller\admin;

use app\BaseController;
use app\model\{MessageTemplateConfig, MessageRecord};
use think\facade\Db;
use think\Request;

class MessageTemplateConfigController extends BaseController
{
    /**
     * 获取模板配置列表
     */
    public function index(Request $request)
    {
        $where = [];
        
        // 商家ID筛选
        if ($merchant_id = $request->param('merchant_id')) {
            $where[] = ['merchant_id', '=', $merchant_id];
        }
        
        // 消息类型筛选
        if ($message_type = $request->param('message_type')) {
            $where[] = ['message_type', '=', $message_type];
        }
        
        $list = MessageTemplateConfig::where($where)
            ->with(['merchant', 'messageSetting'])
            ->order('id', 'desc')
            ->paginate($request->param('limit', 15));
            
        return $this->success('获取成功', $list);
    }
    
    /**
     * 添加模板配置
     */
    public function save(Request $request)
    {
        $data = $this->validate($request->post(), [
            'merchant_id' => 'require|integer',
            'message_setting_id' => 'require|integer',
            'message_type' => 'require|integer',
            'template_id' => 'require',
            'template_content' => 'require',
            'status' => 'require|integer'
        ]);
        
        Db::startTrans();
        try {
            MessageTemplateConfig::create($data);
            
            Db::commit();
            return $this->success('添加成功');
        } catch (\Exception $e) {
            Db::rollback();
            return $this->error($e->getMessage());
        }
    }
    
    /**
     * 更新模板配置
     */
    public function update(Request $request, $id)
    {
        $data = $this->validate($request->post(), [
            'merchant_id' => 'require|integer',
            'message_setting_id' => 'require|integer',
            'message_type' => 'require|integer',
            'template_id' => 'require',
            'template_content' => 'require',
            'status' => 'require|integer'
        ]);
        
        Db::startTrans();
        try {
            MessageTemplateConfig::update($data, ['id' => $id]);
            
            Db::commit();
            return $this->success('更新成功');
        } catch (\Exception $e) {
            Db::rollback();
            return $this->error($e->getMessage());
        }
    }
    
    /**
     * 删除模板配置
     */
    public function delete($id)
    {
        Db::startTrans();
        try {
            // 检查是否有发送记录
            $count = MessageRecord::where('message_template_id', $id)->count();
            if ($count > 0) {
                throw new \Exception('该模板已有发送记录,不能删除');
            }
            
            MessageTemplateConfig::destroy($id);
            
            Db::commit();
            return $this->success('删除成功');
        } catch (\Exception $e) {
            Db::rollback();
            return $this->error($e->getMessage());
        }
    }
    
    /**
     * 获取发送记录
     */
    public function records(Request $request, $id)
    {
        $list = MessageRecord::where('message_template_id', $id)
            ->with(['user'])
            ->order('id', 'desc')
            ->paginate($request->param('limit', 15));
            
        return $this->success('获取成功', $list);
    }
} 